/* Copyright (c) 2001 Nextel Communications, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of Nextel Communications, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEXTEL
 * COMMUNICATIONS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.nextel.ui; // Generated package name
import com.nextel.ui.custom.OListenerScroll;
import com.nextel.ui.custom.WordWrap;
import com.nextel.util.StringUtils;
import com.nextel.util.lwuit.Font;
//import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.List;

/**
 * Provides a text label for display on the screen.  A label is not focusable,
 * meaning that it does not receive key press events.
 *
 * @author Glen Cordrey
 */
public class OLabel extends OComponent implements OListenerScroll {

    private int height;
    private int width;
    private List label;
    private Font labelFont;
    private int posicionTravers = 0;
    private int sizeContainer = 0;
    public int lineasMonstrar = 0;

    public int getPosicionTravers() {
        return posicionTravers;
    }

    public void addPosicionTravers(int keyCode) {
//        System.out.println("addPosicionTravers="+ keyCode);

        if (keyCode == OAbstractScreen.UP_KEY && posicionTravers > 0) {
            posicionTravers = posicionTravers - 1;
            repaint();
        } else if (alturaVistaOffset() && keyCode == OAbstractScreen.DOWN_KEY && posicionTravers < label.size()) {
            posicionTravers = posicionTravers + 1;
            repaint();
        }
    }

    public boolean alturaVistaOffset() {

        int alturaPorMostrar = (getY() + height - getContainer().getScreen().getBodyHeight()) - posicionTravers * labelFont.getHeight();
        return alturaPorMostrar > 0;
    }

    public void setLabel(String text) {

        if (label != null) {
            label.clear();
        }

        calcularTexto(text);

        repaint();
    }

    public void setLabel(String text, int plineasMostrar) {
        if (label != null) {
            label.clear();
        }

        this.lineasMonstrar = plineasMostrar;

        calcularTexto(text);
      
        repaint();
    }

    /**
     * Creates a new <code>OLabel</code> instance.
     *
     * @param text The text for the label.  The text can be multiple lines, with
     * \n characters used to indicate line breaks. It is up to the caller to
     * ensure that lines do not exceed the screen width, as this class does not
     * validate the width.
     * @param textFont The font to use for the label text.
     */
    public OLabel(String text, Font textFont) {
        labelFont = textFont;
        label = new ArrayList();
        sizeContainer = 0;
        calcularTexto(text);

        
    } // constructor

    public OLabel(String text, Font textFont, int psizeContainer) {
        labelFont = textFont;
        label = new ArrayList();
        sizeContainer = psizeContainer;
        calcularTexto(text);

        
    }

    /**
     * Paints the label to the screen.
     *
     * @param g The Graphics object to render to.
     */
    public void paint(Graphics g) {
  
        int oldColor = g.getColor();
        int fontHeight = labelFont.getHeight();
        int y = getY() + OUILook.STRING_SPACER_HEIGHT;
 
        if (label.size() > 0) {
            g.setColor(OColor.CUERPO_BACKGROUND);
            g.fillRect(getX(), y, getWidth(), fontHeight * label.size());
        }

        com.nextel.util.lwuit.Graphics grap = new com.nextel.util.lwuit.Graphics(g);
        grap.setFont(labelFont);

        g.setColor(OUILook.TEXT_COLOR);
        grap.setColor(OUILook.TEXT_COLOR);
        
        for (int idx = posicionTravers; idx < lineasMonstrar && idx < label.size(); idx++) {

            grap.drawString((String) label.get(idx), getX(), y);
            y += fontHeight;
        }

        g.setColor(oldColor);
        // Logger.dev( "OLabel.paint EXITTING" );
    } // paint

    /**
     * Gets the height of the label.
     *
     * @return an <code>int</code> value
     */
    public int getHeight() {
//        if (Debug.ON) {
//            Logger.dev("OLabel.getHeight ENTERED");
//        }
        return height;
    } // getHeight

    /**
     * Gets the width of the label.  If the label contains multiple lines then the
     * width of the widest line is the width of the labe.
     *
     * @return an <code>int</code> value
     */
    public int getWidth() {
//        if (Debug.ON) {
//            Logger.dev("OLabel.getWidth ENTERED");
//        }
        return width;
    } // getWidth

    private void calcularTexto(String textoFinal) {

        String[] labelTemp = StringUtils.breakIntoLines(textoFinal);


        for (int idx = 0; idx < labelTemp.length; idx++) {
           // System.out.println(" labelTemp[idx] =" + labelTemp[idx]);
            obtenerListaWrap(label, labelTemp[idx], labelFont);
        }

        for (int idx = 0; idx < label.size(); idx++) {
            int lineWidth = labelFont.stringWidth((String) label.get(idx));
            if (lineWidth > width) {
                width = lineWidth;
            }
        }

        if(lineasMonstrar <= 0 ){
            lineasMonstrar = label.size();
        }

        height = lineasMonstrar * labelFont.getHeight() + OUILook.STRING_SPACER_HEIGHT;
       
//        System.out.println("label=" + label.size());
//        System.out.println("height=" + height);

    }

    public void obtenerListaWrap(List lista, String text, Font textFont) {

        if (sizeContainer > 0) {
            WordWrap ww = new WordWrap(textFont, text, sizeContainer - OUILook.H_GAP);
            int numeroLineas = ww.numeroLineas();
            if (numeroLineas > 1) {
                int si = 0;
                int ei = 0;
                while (true) {
                    int np = ww.next();
                    if (np == -1) {
                        break;
                    } else {
                        si = ei;
                        ei = np;
                    }

                    String lineText = text.substring(si, ei);
//                    System.out.println("lineText=" + lineText);
                    lista.add(lineText);
                }
            } else {
//                System.out.println("text=" + text);
                lista.add(text);
            }
        } else {
            lista.add(text);
        }
    }
}// OLabel

